package org.glickr.api.people

import groovy.util.slurpersupport.GPathResult
import org.glickr.api.FlickrApiMethod
import org.glickr.FlickrPeople
import org.glickr.FlickrServicePluginException
import org.glickr.FlickrServiceApiException

/* ----------------------------------------------------------------------------------------------------
REQUEST: flickr.people.getInfo
INFO: Get information about a user.

AUTHENTICATION: This method does not require authentication.

ARGUMENTS:
api_key (Required): Your API application key.
user_id (Required): The NSID of the user to fetch information about.

EXAMPLE RESPONSE:
<person nsid="12037949754@N01" ispro="0" iconserver="122" iconfarm="1">
  <username>bees</username>
  <realname>Cal Henderson</realname>
  <mbox_sha1sum>eea6cd28e3d0003ab51b0058a684d94980b727ac</mbox_sha1sum>
  <location>Vancouver, Canada</location>
  <photosurl>http://www.flickr.com/photos/bees/</photosurl>
  <profileurl>http://www.flickr.com/people/bees/</profileurl>
  <photos>
    <firstdate>1071510391</firstdate>
    <firstdatetaken>1900-09-02 09:11:24</firstdatetaken>
    <count>449</count>
  </photos>
</person>
The firstdate element contains the unix timestamp of the first photo uploaded by the user. The firstdatetaken element contains the mysql datetime of the first photo taken by the user.

The iconserver element is used to build the url to the users' buddyicon - for more information please read the buddyicon guide.


If the API call is authenticated contact information will also be returned as attributes on the person element.  contact, friend, and family are boolean flags describing the relationship between the authenticated user, and the person currently being inspected.   revcontact, revfriend, and revfamily is the reciprocal relationship.
---------------------------------------------------------------------------------------------------- */

class peopleGetInfo implements FlickrApiMethod {
    //
    //  API METHOD
    //
    static final String apiMethod = 'flickr.people.getInfo'

    //
    //  VALIDATOR
    //
    def validatorClosure = { FlickrPeople people ->
        if (!people || !people?.nsid) { return [validated:false, message:"Required value 'FlickrPeople.nsid' is missing"] }
        return [validated:true]
    }

    //
    //  PARAMS
    //
    def paramsClosure = { FlickrPeople people ->
        [user_id:people?.nsid]
    }

    //
    //  PROCESSOR
    //
    def processorClosure = { GPathResult response, FlickrPeople people ->
        people.id           = response.person.@id.toString()
        people.nsid         = response.person.@nsid.toString()
        people.pathAlias    = response.person.@path_alias.toString()
        people.gender       = response.person.@gender.toString() ?: null       // todo enum
        people.isIgnored    = (response.person.@ignored?.toString()?.equals('1'))
        people.isContact    = (response.person.@contact?.toString()?.equals('1'))
        people.isFriend     = (response.person.@friend?.toString()?.equals('1'))
        people.isFamily     = (response.person.@family?.toString()?.equals('1'))
        people.isRevContact = (response.person.@revcontact?.toString()?.equals('1'))
        people.isRevFriend  = (response.person.@revfriend?.toString()?.equals('1'))
        people.isRevFamily  = (response.person.@revfamily?.toString()?.equals('1'))
        people.isPro        = (response.person.@ispro?.toString() == '1')
        people.iconServer   = response.person.@iconserver.toLong()
        people.iconFarm     = response.person.@iconfarm.toLong()
        people.username     = response.person.username.toString()
        people.realname     = response.person.realname.toString()
        people.mboxSha1sum  = response.person.mbox_sha1sum.toString()
        people.location     = response.person.location.toString()
        people.description  = response.person.description.toString()
        people.photosUrl    = response.person.photosurl.toString()
        people.profileUrl   = response.person.profileurl.toString()
        people.mobileUrl    = response.person.mobileurl.toString()
        people.timezoneLabel        = response.person.timezone.@label.toString()        // todo convert to Groovy
        people.timezoneOffset       = response.person.timezone.@offset.toString()       // todo convert to Groovy
        people.photosFirstDate      = response.person.photos.firstdate.toString()       // todo convert to Groovy
        people.photosFirstDateTaken = response.person.photos.firstdatetaken.toString()  // todo convert to Groovy
        people.photosCount          = response.person.photos.count.toInteger()
        return people
    }

    //
    //  ERROR CODES
    //
    Closure errorsClosure = { GPathResult response ->
        if (response.err.@code.toInteger() == 1) { return null }      // user not found

        // recoverable errors
        if ([100,105,116].contains(response.err.@code.toInteger())) {
            return new FlickrServiceApiException(response)
        } else {
            return new FlickrServicePluginException(response)
        }
    }
}